/*
 * Created by JFormDesigner on Thu Jun 23 13:19:42 CST 2022
 */

package jw3.c10.view;


import jw3.c10.controllner.RoleController;
import jw3.c10.controllner.StationController;
import jw3.c10.model.Role;
import jw3.c10.model.Station;
import jw3.c10.utils.DBConnection;

import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author Brainrain
 */
public class Route extends JFrame {
    public Route() {
        initComponents();
        this.setSize(800,900);
        table1.setSize(600,500);
        panel1.setSize(600,500);
        this.setVisible(true);
        dialog1.setSize(500,600);

    }

    private void menu1MouseClicked(MouseEvent e) {
        // TODO add your code here
        //线路管理
//        dialog1.setModal(true);
//        dialog1.setVisible(true);
        showData(textField2.getText());
    }
    public void showData(String name){
        String sql="select a.SID,b.RName,a.Station  from station a join role b ON a.RID=b.RID where Station like ? ";
        try {
            Connection conn= DBConnection.getConnection();
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,"%"+name+"%");
            ResultSet rs=ps.executeQuery();
            data=new Vector<Vector<Object>>();
            while (rs.next()){
                Vector<Object> data1=new Vector<Object>();
                data1.add(rs.getInt("SID"));
                data1.add(rs.getString("RName"));
                data1.add(rs.getString("Station"));
                data.add(data1);
            }
            DBConnection.close(rs,ps,conn);
            Vector<String> heads=new Vector<String>();

            heads.add("编号");
            heads.add("线路号");
            heads.add("站点名称");
            DefaultTableModel dtm=new DefaultTableModel(data,heads);
           table1.setModel(dtm);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void button1MouseClicked(MouseEvent e) {
        // TODO add your code here
        //模糊查  站点
        showData(textField2.getText());
    }

    private void button2MouseClicked(MouseEvent e) {
        // TODO add your code here
//        添加路线
dialog1.setModal(true);
dialog1.setVisible(true);

    }




    private void button5MouseClicked(MouseEvent e) {
        // TODO add your code here
        //关闭 取消
        dialog1.dispose();
    }

    private void button6MouseClicked(MouseEvent e) {
        // TODO add your code here
//        确定
        int  r;
        String s;
       r=comboBox1.getSelectedIndex()+1;
        s=textField3.getText();
       if (StationController.zj(r,s)){
           showData("");
           JOptionPane.showMessageDialog(null,"更新成功");
       }else{
           JOptionPane.showMessageDialog(null,"更新失败");
       }
    }

    private void button3MouseClicked(MouseEvent e) {
        // TODO add your code here

        //删除
        int index=table1.getSelectedRow();//获取选择第一行的所索引
        if (index!=-1){//是否选中了一行
            if (JOptionPane.showConfirmDialog(null,
                    "您是否确认删除此数据?")==0) {//是否点击了确定

                String sql = "delete from Station where SID=?";

                int id = (int) data.get(index).get(0);
                //调用控制器完成删除
                Connection conn = null;

                try {
                    conn = DBConnection.getConnection();
                    PreparedStatement ps = conn.prepareStatement(sql);
                    ps.setInt(1, id);
                    if (ps.executeUpdate() > 0) {
                        showData("");//自动刷新
                        JOptionPane.showMessageDialog(null,
                                "删除成功!");

                    } else {
                        JOptionPane.showMessageDialog(null,
                                "删除失败!");
                    }
                    DBConnection.close(null, ps, conn);

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            }else{
                JOptionPane.showMessageDialog(null,
                        "您未选中任意一行!");
            }
        }



    private void button4MouseClicked(MouseEvent e) {
        // TODO add your code here

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label3 = new JLabel();
        textField2 = new JTextField();
        panel1 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        button1 = new JButton();
        button2 = new JButton();
        button3 = new JButton();
        dialog1 = new JDialog();
        comboBox1 = new JComboBox();
        label4 = new JLabel();
        label5 = new JLabel();
        textField3 = new JTextField();
        button5 = new JButton();
        button6 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label3 ----
        label3.setText("\u8bf7\u8f93\u5165\u7ad9\u70b9\u540d\u79f0");
        contentPane.add(label3);
        label3.setBounds(20, 35, 105, label3.getPreferredSize().height);
        contentPane.add(textField2);
        textField2.setBounds(125, 25, 135, textField2.getPreferredSize().height);

        //======== panel1 ========
        {
            panel1.setLayout(new GridLayout());

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(table1);
            }
            panel1.add(scrollPane1);
        }
        contentPane.add(panel1);
        panel1.setBounds(new Rectangle(new Point(45, 125), panel1.getPreferredSize()));

        //---- button1 ----
        button1.setText("\u641c\u7d22");
        button1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button1MouseClicked(e);
            }
        });
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(275, 25), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u6dfb\u52a0");
        button2.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button2MouseClicked(e);
            }
        });
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(370, 25), button2.getPreferredSize()));

        //---- button3 ----
        button3.setText("\u5220\u9664");
        button3.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                button3MouseClicked(e);
            }
        });
        contentPane.add(button3);
        button3.setBounds(new Rectangle(new Point(465, 25), button3.getPreferredSize()));

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());

        //======== dialog1 ========
        {
            Container dialog1ContentPane = dialog1.getContentPane();
            dialog1ContentPane.setLayout(null);
            dialog1ContentPane.add(comboBox1);
            comboBox1.setBounds(110, 25, 250, comboBox1.getPreferredSize().height);

            //---- label4 ----
            label4.setText("\u7ebf\u53f7\u9009\u62e9");
            dialog1ContentPane.add(label4);
            label4.setBounds(20, 30, 75, label4.getPreferredSize().height);

            //---- label5 ----
            label5.setText("\u7ad9\u70b9\u540d\u5b57");
            dialog1ContentPane.add(label5);
            label5.setBounds(30, 80, 75, label5.getPreferredSize().height);
            dialog1ContentPane.add(textField3);
            textField3.setBounds(130, 75, 165, textField3.getPreferredSize().height);

            //---- button5 ----
            button5.setText("\u53d6\u6d88");
            button5.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button5MouseClicked(e);
                }
            });
            dialog1ContentPane.add(button5);
            button5.setBounds(new Rectangle(new Point(190, 160), button5.getPreferredSize()));

            //---- button6 ----
            button6.setText("\u786e\u5b9a");
            button6.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    button5MouseClicked(e);
                    button6MouseClicked(e);
                }
            });
            dialog1ContentPane.add(button6);
            button6.setBounds(35, 160, 78, 30);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < dialog1ContentPane.getComponentCount(); i++) {
                    Rectangle bounds = dialog1ContentPane.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = dialog1ContentPane.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                dialog1ContentPane.setMinimumSize(preferredSize);
                dialog1ContentPane.setPreferredSize(preferredSize);
            }
            dialog1.pack();
            dialog1.setLocationRelativeTo(dialog1.getOwner());
        }
        // JFormDesigner - End of component initialization  //GEN-END:initComponents

showData("");
        //下拉框传值
        List<Role> k= RoleController.cc();
        for (int i = 0; i < k.size(); i++) {
            comboBox1.addItem(k.get(i).getRName());
        }
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label3;
    private JTextField textField2;
    private JPanel panel1;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JDialog dialog1;
    private JComboBox comboBox1;
    private JLabel label4;
    private JLabel label5;
    private JTextField textField3;
    private JButton button5;
    private JButton button6;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    private  static Vector<Vector<Object>> data;
}
